<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI文本生成器</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            color: #333;
            min-height: 100vh;
            padding: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        .container {
            width: 90%;
            max-width: 800px;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
            overflow: hidden;
            padding: 30px;
        }
        
        header {
            text-align: center;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 1px solid #eee;
        }
        
        h1 {
            color: #2c3e50;
            font-size: 2.5rem;
            margin-bottom: 10px;
            background: linear-gradient(90deg, #6a11cb, #2575fc);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
        }
        
        .subtitle {
            color: #7f8c8d;
            font-size: 1.1rem;
        }
        
        .input-group {
            margin-bottom: 25px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #2c3e50;
        }
        
        input, textarea, select {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            font-size: 1rem;
            transition: all 0.3s;
        }
        
        input:focus, textarea:focus, select:focus {
            border-color: #6a11cb;
            outline: none;
            box-shadow: 0 0 0 3px rgba(106, 17, 203, 0.1);
        }
        
        textarea {
            min-height: 150px;
            resize: vertical;
        }
        
        .form-row {
            display: flex;
            gap: 20px;
            margin-bottom: 25px;
        }
        
        .form-row .input-group {
            flex: 1;
            margin-bottom: 0;
        }
        
        button {
            background: linear-gradient(90deg, #6a11cb, #2575fc);
            color: white;
            border: none;
            padding: 15px 30px;
            border-radius: 10px;
            font-size: 1.1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            width: 100%;
            margin-top: 10px;
        }
        
        button:hover {
            transform: translateY(-3px);
            box-shadow: 0 7px 15px rgba(106, 17, 203, 0.4);
        }
        
        button:active {
            transform: translateY(0);
        }
        
        .preview-section {
            margin-top: 30px;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 10px;
            display: none;
        }
        
        .preview-section h3 {
            margin-bottom: 15px;
            color: #2c3e50;
        }
        
        .preview-content {
            max-height: 200px;
            overflow-y: auto;
            padding: 15px;
            background: white;
            border-radius: 8px;
            border: 1px solid #e0e0e0;
        }
        
        .file-list {
            margin-top: 30px;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 10px;
        }
        
        .file-list h3 {
            margin-bottom: 15px;
            color: #2c3e50;
        }
        
        .file-item {
            padding: 12px 15px;
            background: white;
            border-radius: 8px;
            margin-bottom: 10px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-left: 4px solid #6a11cb;
        }
        
        .file-name {
            font-weight: 500;
        }
        
        .file-date {
            color: #7f8c8d;
            font-size: 0.9rem;
        }
        
        .notification {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 15px 25px;
            background: #2ecc71;
            color: white;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
            transform: translateX(150%);
            transition: transform 0.4s;
            z-index: 1000;
        }
        
        .notification.show {
            transform: translateX(0);
        }
        
        .loading {
            display: none;
            text-align: center;
            margin: 20px 0;
        }
        
        .spinner {
            border: 5px solid #f3f3f3;
            border-top: 5px solid #6a11cb;
            border-radius: 50%;
            width: 50px;
            height: 50px;
            animation: spin 1s linear infinite;
            margin: 0 auto;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        footer {
            text-align: center;
            margin-top: 30px;
            color: #7f8c8d;
            font-size: 0.9rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>AI文本生成器</h1>
            <p class="subtitle">输入提示词，生成高质量文本内容并保存为文件</p>
        </header>
        
        <main>
            <div class="input-group">
                <label for="prompt">文本提示词</label>
                <textarea id="prompt" placeholder="请输入您想要生成的文本主题或关键词...">人工智能对未来教育的影响</textarea>
            </div>
            
            <div class="form-row">
                <div class="input-group">
                    <label for="wordCount">文本字数</label>
                    <select id="wordCount">
                        <option value="200">200字</option>
                        <option value="500" selected>500字</option>
                        <option value="800">800字</option>
                        <option value="1000">1000字</option>
                    </select>
                </div>
                
                <div class="input-group">
                    <label for="fileName">文件名称</label>
                    <input type="text" id="fileName" placeholder="输入文件名（不含扩展名）" value="AI教育文章">
                </div>
            </div>
            
            <div class="loading" id="loading">
                <div class="spinner"></div>
                <p>正在生成文本内容，请稍候...</p>
            </div>
            
            <button id="generateBtn">生成文本内容</button>
            
            <div class="preview-section" id="previewSection">
                <h3>内容预览</h3>
                <div class="preview-content" id="previewContent"></div>
                <button id="saveBtn">保存为TXT文件</button>
            </div>
            
            <div class="file-list">
                <h3>已保存的文件</h3>
                <div id="fileListContainer">
                    <!-- 文件列表将通过JS动态生成 -->
                    <p class="no-files">暂无保存的文件</p>
                </div>
            </div>
        </main>
        
        <footer>
            <p>AI文本生成器 &copy; 2023 | 人工智能通识课程实验</p>
        </footer>
    </div>
    
    <div class="notification" id="notification">文件保存成功！</div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const generateBtn = document.getElementById('generateBtn');
            const saveBtn = document.getElementById('saveBtn');
            const previewSection = document.getElementById('previewSection');
            const previewContent = document.getElementById('previewContent');
            const loading = document.getElementById('loading');
            const notification = document.getElementById('notification');
            const fileListContainer = document.getElementById('fileListContainer');
            
            // 模拟AI文本生成函数
            function generateText(prompt, wordCount) {
                // 在实际应用中，这里会调用AI API
                // 这里我们模拟生成文本
                
                const topics = {
                    "人工智能": [
                        "人工智能是当今科技领域最引人注目的技术之一。",
                        "随着深度学习技术的发展，AI在各个行业展现出巨大潜力。",
                        "机器学习算法使计算机能够从数据中学习并做出决策。"
                    ],
                    "教育": [
                        "现代教育正在经历数字化转型。",
                        "个性化学习已成为教育技术的重要方向。",
                        "在线教育平台使知识获取更加便捷和灵活。"
                    ],
                    "未来": [
                        "未来教育将更加注重培养学生的创造力和批判性思维。",
                        "技术将改变传统课堂的教学模式。",
                        "终身学习将成为未来社会的重要特征。"
                    ]
                };
                
                // 根据提示词提取关键词
                const keywords = Object.keys(topics).filter(key => 
                    prompt.toLowerCase().includes(key.toLowerCase())
                );
                
                // 如果没有匹配的关键词，使用默认内容
                if (keywords.length === 0) {
                    keywords.push("人工智能", "教育", "未来");
                }
                
                let generatedText = "";
                const targetLength = parseInt(wordCount);
                
                // 生成文本
                while (generatedText.length < targetLength) {
                    for (const keyword of keywords) {
                        const sentences = topics[keyword];
                        const randomSentence = sentences[Math.floor(Math.random() * sentences.length)];
                        generatedText += randomSentence + " ";
                        
                        if (generatedText.length >= targetLength) break;
                    }
                    
                    // 添加一些连接词使文本更自然
                    if (generatedText.length < targetLength) {
                        generatedText += "此外，值得注意的是，随着技术的不断进步，";
                    }
                }
                
                // 确保文本长度符合要求
                generatedText = generatedText.substring(0, targetLength);
                
                // 添加标题
                return `标题：${prompt}\n\n${generatedText}`;
            }
            
            // 显示通知
            function showNotification(message) {
                notification.textContent = message;
                notification.classList.add('show');
                setTimeout(() => {
                    notification.classList.remove('show');
                }, 3000);
            }
            
            // 更新文件列表
            function updateFileList() {
                // 在实际应用中，这里会从服务器获取文件列表
                // 这里我们模拟一些文件
                const files = [
                    { name: 'AI教育文章.txt', date: '2023-10-15 14:30' },
                    { name: '机器学习概述.txt', date: '2023-10-14 11:20' },
                    { name: '未来科技趋势.txt', date: '2023-10-13 09:45' }
                ];
                
                if (files.length === 0) {
                    fileListContainer.innerHTML = '<p class="no-files">暂无保存的文件</p>';
                    return;
                }
                
                fileListContainer.innerHTML = files.map(file => `
                    <div class="file-item">
                        <div class="file-name">${file.name}</div>
                        <div class="file-date">${file.date}</div>
                    </div>
                `).join('');
            }
            
            // 初始化文件列表
            updateFileList();
            
            // 生成文本按钮点击事件
            generateBtn.addEventListener('click', function() {
                const prompt = document.getElementById('prompt').value;
                const wordCount = document.getElementById('wordCount').value;
                
                if (!prompt.trim()) {
                    alert('请输入文本提示词！');
                    return;
                }
                
                // 显示加载动画
                loading.style.display = 'block';
                generateBtn.disabled = true;
                
                // 模拟AI生成文本的延迟
                setTimeout(() => {
                    const generatedText = generateText(prompt, wordCount);
                    previewContent.textContent = generatedText;
                    previewSection.style.display = 'block';
                    
                    // 隐藏加载动画
                    loading.style.display = 'none';
                    generateBtn.disabled = false;
                    
                    // 滚动到预览区域
                    previewSection.scrollIntoView({ behavior: 'smooth' });
                }, 1500);
            });
            
            // 保存文件按钮点击事件
            saveBtn.addEventListener('click', function() {
                const fileName = document.getElementById('fileName').value;
                const content = previewContent.textContent;
                
                if (!fileName.trim()) {
                    alert('请输入文件名！');
                    return;
                }
                
                // 在实际应用中，这里会将文件保存到服务器
                // 这里我们模拟保存过程
                
                // 创建Blob对象
                const blob = new Blob([content], { type: 'text/plain' });
                
                // 创建下载链接
                const a = document.createElement('a');
                a.href = URL.createObjectURL(blob);
                a.download = `${fileName}.txt`;
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
                
                // 显示成功通知
                showNotification(`文件 ${fileName}.txt 保存成功！`);
                
                // 更新文件列表
                updateFileList();
            });
        });
    </script>
</body>
</html>